﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using HYCodeBase.CommonUtility;
using HYCodeBase.Math;
using HYRevitCode.ElementSearchUtility;
using HYRevitCode.XYZUtility;

namespace Assist
{
	public class AssistFunc
	{
		public static ModelCurve CreateNewCurve(Autodesk.Revit.ApplicationServices.Application app, Autodesk.Revit.DB.Document doc, Curve curve)
		{
			XYZ origin = new XYZ(0.0, 0.0, 0.0);
			XYZ normal = new XYZ(0.0, 0.0, 1.0);
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(normal, origin);
			SketchPlane sketchPlane = doc.CreatYJKSketchPlane(plane);
			return doc.Create.NewModelCurve(curve, sketchPlane);
		}

		public static ModelLine CreateNewLine(Autodesk.Revit.ApplicationServices.Application app, Autodesk.Revit.DB.Document doc, XYZ ptstart, XYZ ptend)
		{
			Line curve = app.CreatYJKLine(ptstart, ptend, true);
			return AssistFunc.CreateNewCurve(app, doc, curve) as ModelLine;
		}

		public static ModelArc CreateNewArc(Autodesk.Revit.ApplicationServices.Application app, Autodesk.Revit.DB.Document doc, XYZ ptstart, XYZ ptend, XYZ pton)
		{
			Arc curve = app.CreatYJKArc(ptstart, ptend, pton);
			return AssistFunc.CreateNewCurve(app, doc, curve) as ModelArc;
		}

		public static void CreateNewArc(Autodesk.Revit.ApplicationServices.Application app, Autodesk.Revit.DB.Document doc, XYZ ptcenter, double radius, double startang, double endang)
		{
			Arc curve = doc.CreatYJKArc(ptcenter, radius, startang, endang, XYZ.BasisX, XYZ.BasisY);
			AssistFunc.CreateNewCurve(app, doc, curve);
		}

		public static void RandomPts(List<XYZ> arpts)
		{
			bool flag = arpts.Count <= 0;
			if (!flag)
			{
				for (int i = 0; i < 2; i++)
				{
					XYZ item = arpts.First<XYZ>();
					arpts.RemoveAt(0);
					arpts.Add(item);
				}
			}
		}

		public static double mmToFeet(double val)
		{
			return UnitUtility.MMToFeet(val);
		}

		public static double feetToMM(double val)
		{
			return UnitUtility.FeetToMM(val);
		}

		public static double feet2ToM2(double val)
		{
			return UnitUtility.SquareFeetToSquareMeter(val);
		}

		public static double m2ToFeet2(double val)
		{
			return UnitUtility.SquareMeterToSquareFeet(val);
		}

		public static double AngleToRadian(double val)
		{
			return UnitUtility.AngleToRadian(val);
		}

		public static double RadianToAngle(double val)
		{
			return UnitUtility.RadianToAngle(val);
		}

		public static bool IsOdd(int nSource)
		{
			return MathUtility.IsOdd(nSource);
		}

		public static string GetAppRootPath()
		{
            return @"C:\Hongye\HYArch2019";


			string text = Assembly.GetExecutingAssembly().Location;
			int length = text.LastIndexOf("\\");
			text = text.Substring(0, length);
			length = text.LastIndexOf("\\");
			return text.Substring(0, length);
		}

		public static ViewType GetCurrentViewType(Autodesk.Revit.DB.Document doc)
		{
			return ElementSearchMethod.GetCurrentViewType(doc);
		}

		public static Level GetCurrentViewLevel(Autodesk.Revit.DB.Document doc)
		{
			return ElementSearchMethod.GetCurrentViewLevel(doc);
		}

		public static List<Level> GetAllLevel(Autodesk.Revit.DB.Document doc)
		{
			return ElementSearchMethod.GetAllLevel(doc);
		}

		public static List<Level> GetAllLevel(Autodesk.Revit.DB.Document doc, Level minLevel, Level maxLevel)
		{
			return ElementSearchMethod.GetAllLevel(doc, minLevel, maxLevel);
		}

		public static Level GetLevelByName(Autodesk.Revit.DB.Document doc, string name)
		{
			return ElementSearchMethod.GetLevelByName(doc, name);
		}

		public static GridType GetGridTypeByName(Autodesk.Revit.DB.Document doc, string name)
		{
			return ElementSearchMethod.GetGridTypeByName(doc, name);
		}

		public static WallType GetWallTypeByName(Autodesk.Revit.DB.Document doc, string name)
		{
			return ElementSearchMethod.GetWallTypeByName(doc, name);
		}

		public static DimensionType GetDimTypeByName(Autodesk.Revit.DB.Document doc, string name, DimensionStyleType styleType)
		{
			return ElementSearchMethod.GetDimTypeByName(doc, name, styleType);
		}

		public static int CompressArray(List<XYZ> arPt)
		{
			return XYZUtilityMethod.CompressArray(arPt);
		}

		public static int CompressArray(List<Element> arElements)
		{
			return GenericUtilityMethod.CompressArrayByContains<Element>(arElements);
		}

		public static int CompressArray(List<Grid> arObj)
		{
			return GenericUtilityMethod.CompressArrayByContains<Grid>(arObj);
		}

		public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
		{
			Bitmap result;
			try
			{
				Bitmap bitmap = new Bitmap(newW, newH);
				Graphics graphics = Graphics.FromImage(bitmap);
				graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
				graphics.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, newW, newH), new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
				graphics.Dispose();
				result = bitmap;
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public static bool LoadStandardFamily(UIApplication uiApp)
		{
			IDictionary<string, string> libraryPaths = uiApp.Application.GetLibraryPaths();
			string text;
			uiApp.Application.GetLibraryPaths().TryGetValue("Metric Library", out text);
			bool flag = string.IsNullOrEmpty(text);
			if (flag)
			{
				text = "c:\\";
			}
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.InitialDirectory = text;
			openFileDialog.Filter = "Family Files (*.rfa)|*.rfa";
			bool flag2 = DialogResult.OK == openFileDialog.ShowDialog();
			bool result;
			if (flag2)
			{
				Family family = null;
				bool flag3 = uiApp.ActiveUIDocument.Document.LoadFamily(openFileDialog.FileName, out family);
				result = flag3;
			}
			else
			{
				result = false;
			}
			return result;
		}

		public static ICollection<ElementId> GetAllFamilyInstance(Autodesk.Revit.DB.Document doc, BuiltInCategory builtInCategory)
		{
			return ElementSearchMethod.GetAllFamilyInstance(doc, builtInCategory);
		}

		public static List<string> GetSystemFontTypeStringArray(Autodesk.Revit.DB.Document doc)
		{
			return ElementSearchMethod.GetSystemFontTypeStringArray(doc);
		}

		public static List<ElementId> SubCollection(List<ElementId> a, List<ElementId> b)
		{
			List<ElementId> list = new List<ElementId>();
			foreach (ElementId item in a)
			{
				bool flag = !b.Contains(item);
				if (flag)
				{
					list.Add(item);
				}
			}
			return list;
		}

		public static double precision = 1E-06;
	}
}
